<script type="text/javascript"> 
    dojo.require("dijit.Tooltip");
    dojo.require("dijit.Menu");   
    window.onload = function() {
        
        var officer = document.getElementById('OfficerUserId');
        
        var group = document.getElementById('Group');   
        var client = document.getElementById('Client');
        
        var addClient  = document.getElementById('AddClient');    
        var viewClient = document.getElementById('ViewClient');
        var editClient = document.getElementById('EditClient');  
        
        var addGroup    = document.getElementById('AddGroup');    
        var deleteGroup = document.getElementById('DeleteGroup');
        var exportGroup = document.getElementById('ExportGroup');  
        var viewGroup   = document.getElementById('ViewGroup');    
        
        /* initialize */
        disableAllButtons();
        if(!group.value) { 
            if(group.options.length > 0) { 
                group.options[0].selected = true;
                clientUpdate(); 
            } else { 
                initGroupButtons();
            }
        } else {
            if(client.value) {
                initGroupButtons();
                initClientButtons();
            } else { 
                clientUpdateCallback();
            }
        }
        
        /* set behaviors */
        group.onchange = function () { disableAllButtons(); clientUpdate(); };
        officer.onchange = function () { updateOpts('CommunityId','/admin/json/get-officer-community-options/id/' + officer.value); return false; };
     
        addClient.onclick  = function () { parent.main.location.href = '/admin/client/add/group/' + group.value; };                
        viewClient.onclick = function () { parent.main.location.href = '/admin/client/view/id/' + client.value; }; 
        editClient.onclick = function () { parent.main.location.href = '/admin/client/add/id/' + client.value; }; 
        
        addGroup.onclick    = function () { groupAdd(); };
        deleteGroup.onclick = function () { groupDelete(); };
        exportGroup.onclick = function () { parent.main.location.href = '/admin/group/export/id/' + group.value; };             
        viewGroup.onclick   = function () { parent.main.location.href = '/admin/group/view/id/' + group.value; }; 
    };  
    
    function disableAllButtons()
    {
        var addGroup    = document.getElementById('AddGroup');
        var deleteGroup = document.getElementById('DeleteGroup');
        var viewGroup   = document.getElementById('ViewGroup');
        var exportGroup = document.getElementById('ExportGroup');
        
        var addClient   = document.getElementById('AddClient');    
        var editClient  = document.getElementById('EditClient');
        var viewClient  = document.getElementById('ViewClient');
        
        addGroup.disabled    = true;
        deleteGroup.disabled = true;
        viewGroup.disabled   = true;
        exportGroup.disabled = true;
        
        addClient.disabled  = true;
        editClient.disabled = true;
        viewClient.disabled = true;
    }
    function initGroupButtons() 
    {
        var addGroup    = document.getElementById('AddGroup');  
        var deleteGroup = document.getElementById('DeleteGroup');    
        var viewGroup   = document.getElementById('ViewGroup');    
        var exportGroup = document.getElementById('ExportGroup');    
        
        var bank   = document.getElementById('BankId');
        var group  = document.getElementById('Group');
        var client = document.getElementById('Client');
        
        if((bank.value)&&(group.options.length < <?php echo Zend_Registry::get('HT_Config')->portfolio->group->max  ?>)) { addGroup.disabled = false; }  
        if((client.options.length === 0)&&(group.value)) { deleteGroup.disabled = false; }  
        if(group.value > 0) { viewGroup.disabled = false; exportGroup.disabled = false; }
    }
    function initClientButtons() 
    {
        var addClient  = document.getElementById('AddClient');    
        var viewClient = document.getElementById('ViewClient');    
        var editClient = document.getElementById('EditClient');    
        
        var client = document.getElementById('Client'); 
        
        if(client.options.length < <?php echo Zend_Registry::get('HT_Config')->portfolio->client->max  ?>) { addClient.disabled = false; }  
        if(client.value > 0) { viewClient.disabled = false; editClient.disabled = false; }  
    }
    function clientUpdate()
    {
        var group = document.getElementById('Group'); 
        updateOpts('Client','/admin/json/get-group-client-options/id/' + group.value, clientUpdateCallback);
    }
    function clientUpdateCallback() 
    { 
        var client = document.getElementById('Client');        
        if(client.options.length > 0) { client.options[0].selected = true; }         
        initGroupButtons();
        initClientButtons();
    }   
    function groupAdd()
    {
        disableAllButtons();
        var bank = document.getElementById('BankId');
        updateOpts('Group','/admin/json/add-group/id/' + bank.value, groupAddCallback);
    }
    function groupAddCallback() 
    {
        var group = document.getElementById('Group'); 
        if(group.options.length > 0) { group.options[0].selected = true; clientUpdate(); }
        group.options[group.options.length - 1].selected = true;
        clientUpdate();
    }    
    function groupDelete()
    {
        disableAllButtons();
        var group = document.getElementById('Group'); 
        updateOpts('Group','/admin/json/deactivate-group/id/' + group.value, groupDeleteCallback);
    }
    function groupDeleteCallback() 
    {
        var group = document.getElementById('Group'); 
        if(group.options.length > 0) { group.options[0].selected = true; clientUpdate(); }
        disableAllButtons(); 
        clientUpdate();
    } 
    function updateOpts(tg,url,callback)
    {
        dojo.xhrGet({
            url: url,
            handleAs: "json",
            timeout: 5000,
            load: function(response, ioArgs) {                
                var obj = dojo.byId(tg);
                obj.options.length = 0;                
                for (index = 0; index < response.length; ++index) {
                    option = response[index];
                    obj.options.add(new Option(option.oT, option.oV));
                }   
                callback();
                return response;
            },
            error: function(response, ioArgs) {
                //alert('Failed xhrGet');
                return response;
            }
        });
    }
</script>

<table>
    <tr>
        <td valign="top">
            <?php echo $this->form ?>
        </td>
        <td valign="top">
            <?php echo $this->formTool ?>
        </td>
    </tr>
</table>

